import java.io.File;
import processing.core.*;
import toxi.geom.*;

/** tObjects are any objects on the page besides curves and text.  For now I am using it to represent
 *  different files such as images, but later there may be movable textboxes and stuff like that as well. 
 *  this should eventually extend TUIOObject
 * 
 * **/
public class tObject {
  private File floc;
  private PApplet parent;
  private int[] wid = new int[1000], hei = new int[1000];
  private Vec3D[] position = new Vec3D[1000];;
  private int pts; 
  private int type; //determines how this object is displayed
  private static final int ICON = 0, TEXT = 1, IMAGE = 2;
  private PImage icon, genericIcon;
  
  public tObject (PApplet p, File _file, Vec3D loc, int _type){
    parent = p;
    pts = 0;
    floc = _file;
    position[0] = new Vec3D(100,100,0);
    genericIcon = parent.loadImage("genericIcon.png");
  }
  
  //this constructor is called when the type is an image
  public tObject (PApplet p, PImage img, Vec3D loc, File _floc) {
    parent = p;
    position[0] = loc;
    icon = img;
    type = 2;
    pts = 0;
    floc = _floc;
    PApplet.println("made it into the constructor!");
  }
  
  public void setIcon(PImage mj) {
    icon = mj;
  }
  
  public tObject (PApplet p, String encodedObject) {
    parent = p;
    String[] basicElements = parent.split(encodedObject, '/');
    if(!basicElements[1].equalsIgnoreCase("tObject")) {
      parent.println("There is a problem decoding a tObject.");
      return;
    } else {
      type = new Integer(basicElements[2]).intValue();
      floc = new File(basicElements[3]);
      int tx = new Integer(basicElements[4]).intValue();
      int ty = new Integer(basicElements[5]).intValue();
      wid[0] = new Integer(basicElements[6]).intValue();
      hei[0] = new Integer(basicElements[7]).intValue();
      position[0] = new Vec3D(tx,ty,0);
    }
  }
  
  public void draw() {
   
    if (icon != null) {
      parent.image(icon, position[0].x, position[0].y);
    } else {
      parent.image(genericIcon, position[0].x, position[0].y);
      parent.text(floc.toString(), position[0].x + (genericIcon.width / 10), position[0].y + (genericIcon.height /2));
    }
    
  }
  
  public String toString() {
    String tmp = null;
    tmp = "/tObject/" + type + "/" + floc.toString() + "/" + position[pts].x + "/" + position[pts].y + "/" + wid[pts] + "/" + hei[pts];
    return tmp;
  }
  

}
